import 'package:flutter/material.dart';
import 'package:surfaceim/common/langs/translation_service.dart';
import 'package:surfaceim/common/routes/pages.dart';
import 'package:surfaceim/common/store/store.dart';
import 'package:surfaceim/common/style/style.dart';
import 'package:surfaceim/common/utils/utils.dart';
import 'package:surfaceim/global.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:phone_form_field/l10n/generated/phone_field_localization.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

Future<void> main() async {
  await Global.init();
  await UserStore.to.getProfile();

  /// 字典型 == 用户配置
  Dictionaries.to.conf();

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: Size(750, 1334),
      builder: () => RefreshConfiguration(
        headerBuilder: () => ClassicHeader(),
        footerBuilder: () => ClassicFooter(),
        hideFooterWhenNotFull: true,
        headerTriggerDistance: 80,
        maxOverScrollExtent: 100,
        footerTriggerDistance: 150,
        child: GestureDetector(
          // 透明也响应处理
          behavior: HitTestBehavior.translucent,
          onTap: () {
            //触摸收起键盘
            FocusScopeNode currentFocus = FocusScope.of(context);
            if (!currentFocus.hasPrimaryFocus &&
                currentFocus.focusedChild != null) {
              FocusManager.instance.primaryFocus!.unfocus();
            }
          },
          child: GetMaterialApp(
            title: 'DHL',
            theme: AppTheme.light,
            debugShowCheckedModeBanner: false,
            initialRoute: AppPages.INITIAL,
            getPages: AppPages.routes,
            builder: EasyLoading.init(),
            translations: TranslationService(),
            navigatorObservers: [AppPages.observer],
            localizationsDelegates: [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
              PhoneFieldLocalization.delegate,
            ],
            supportedLocales: ConfigStore.to.languages,
            locale: ConfigStore.to.locale,
            fallbackLocale: Locale('en', 'US'),
            enableLog: true,
            logWriterCallback: Logger.write,
          ),
        ),
      ),
    );
  }
}
